<?php
namespace Home\Controller;
use Think\Controller;
class OrderController extends Controller{
	public function add(){
		$id = session('member_id');
		if(!$id){
			session('returnURL',U('Order/add'));
			$this->error('购买前请先登录',U('login/login'));
		}
		//echo $id;die();
		$cartModel = D('cart');
		$cartData = $cartModel->cartlist();
		//var_dump($cartData);die();
		//var_dump(session('member_id'));
		//var_dump(session('member_name'));die();
		//生成订单
		$tp = 0;
		foreach($cartData as $k=>$v){
			$tp = $v['price']*$v['goods_number'];
			//echo $tp;
		}
		$orderModel = D('Order');
		$orderId = $orderModel->add(array(
			'member_id'=>$id,
			'addtime'=>time(),
			'total_price'=>$tp,
		));
		//var_dump($orderId);die();
		if($orderId){
			//把订单的数据 放到订单商品中
			$ogModel = D('order_goods');
			foreach ($cartData as $k=>$v){
			$ogModel->add(array(
				'goods_id' =>$v['goods_id'],
				'member_id'=>$id,
				'order_id'=>$orderId,
				'goods_number'=>$v['goods_number'],
				'price' => $v['price'],
			));
			}
		}
		//清空购物车
		
		$cartModel->clear();
		$this->success('亲,下单成功',U('Index/index'));
		exit;
	}
    
	
	
	
}